//
//  BaseRefreshCollectionViewController.m
//  sharedBooks
//
//  Created by XunFengKJ on 2017/6/26.
//  Copyright © 2017年 XunFengKJ. All rights reserved.
//

#import "BaseRefreshCollectionViewController.h"
#import "MJRefresh.h"
@interface BaseRefreshCollectionViewController ()
/**
 *  是否是上拉
 */

@property (nonatomic, assign) BOOL isHeaderRereshing;
@end

@implementation BaseRefreshCollectionViewController

#pragma mark - Public Methods

- (void)startPullDownRefreshing {
    [self.collectionView.mj_header beginRefreshing];
    if ([self.collectionView.mj_header isRefreshing]) {
        [self headerRereshing];
    }
}

- (void)addToDataSource:(NSArray *)requestData{
    //刷新与下拉分开载入
    if (self.requestCurrentPage > 1) {
        //增加数据源
        [self.dataSource addObjectsFromArray:requestData];
        //刷新
        [self reloadDataWithBlank];
        //结束Loading
        [self endLoadMoreRefreshing];
    } else {
        //载入数据源
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:requestData];
        //刷新
        [self reloadDataWithBlank];
        //结束Loading
        [self endPullDownRefreshing];
    }
}

- (void)addToMultiDataSource:(NSArray *)requestData{
    //刷新与下拉分开载入
    if (self.requestCurrentPage > 1) {
        //增加数据源
        for (int i=0; i<requestData.count; i++) {
            [self.dataSource[i] addObjectsFromArray:[requestData objectAtIndex:i]];
        }
        //刷新
        [self reloadDataWithBlank];
        //结束Loading
        [self endLoadMoreRefreshing];
    } else {
        //载入数据源
        for (int i=0; i<requestData.count; i++) {
            self.dataSource[i] = [NSMutableArray array];
            [self.dataSource[i] addObjectsFromArray:[requestData objectAtIndex:i]];
        }
        //刷新
        [self reloadDataWithBlank];
        //结束Loading
        [self endPullDownRefreshing];
    }
}

- (void)addToMultiDataSource:(NSArray *)requestData section:(NSInteger)section{
    //刷新与下拉分开载入
    if (self.requestCurrentPage > 1) {
        //增加数据源
        [self.dataSource[section] addObjectsFromArray:requestData];
        //刷新
        [self reloadDataWithBlank];
        //结束Loading
        [self endLoadMoreRefreshing];
    } else {
        //载入数据源
        self.dataSource[section] = [NSMutableArray array];
        [self.dataSource[section] addObjectsFromArray:requestData];
        //刷新
        [self reloadDataWithBlank];
        //结束Loading
        [self endPullDownRefreshing];
    }
}

#pragma mark - Private Methods

- (void)endPullDownRefreshing {
    [self.collectionView.mj_header endRefreshing];
}

- (void)endLoadMoreRefreshing {
    [self.collectionView.mj_footer endRefreshing];
}

#pragma mark - Data Source

-(void)loadDataSourceFail{
    if (_isHeaderRereshing) {
        [self.collectionView.mj_header endRefreshing];
    }else{
        [self.collectionView.mj_footer endRefreshing];
    }
}

#pragma mark - Propertys

-(void)setIsPageEnd:(BOOL)isPageEnd{
    _isPageEnd=isPageEnd;
    if (_isPageEnd) {
        self.collectionView.mj_footer.hidden=YES;
    }else{
        self.collectionView.mj_footer.hidden=NO;
    }
}

-(void)setIsHeaderHidden:(BOOL)isHeaderHidden{
    _isHeaderHidden=isHeaderHidden;
    if (_isHeaderHidden) {
        self.collectionView.mj_header.hidden=YES;
    }else{
        self.collectionView.mj_header.hidden=NO;
    }
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载Header
    //    BaseRefreshTableViewHeader *header = [BaseRefreshTableViewHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    //    header.lastUpdatedTimeLabel.hidden = YES;
    //    header.stateLabel.hidden = YES;
    //    self.tableView.header = header;
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    //加载Footer
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
    self.collectionView.mj_footer.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - MJRefresh

//下拉刷新
-(void)headerRereshing{
    _isHeaderRereshing=YES;
    self.requestCurrentPage = 1;
    [self loadDataSource];
}

//上拉加载
-(void)footerRereshing{
    _isHeaderRereshing=NO;
    self.requestCurrentPage ++;
    [self loadDataSource];
}

@end
